package cn.tannn.ops.ssh.controller;


import cn.tannn.jdevelops.annotations.web.mapping.PathRestController;
import cn.tannn.jdevelops.jpa.constant.SQLOperator;
import cn.tannn.jdevelops.jpa.result.JpaPageResult;
import cn.tannn.jdevelops.result.response.ResultPageVO;
import cn.tannn.jdevelops.result.response.ResultVO;
import cn.tannn.jdevelops.result.utils.ListTo;
import cn.tannn.ops.ssh.controller.dto.SshInfoAdd;
import cn.tannn.ops.ssh.controller.dto.SshInfoEdit;
import cn.tannn.ops.ssh.controller.dto.SshInfoPage;
import cn.tannn.ops.ssh.entity.SshInfo;
import cn.tannn.ops.ssh.service.SshInfoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;

/**
 * 服务器ssh连接信息
 *
 * @author tnnn
 * @version V1.0
 * @date 2024-9-10
 */
@PathRestController("sshInfo")
@Slf4j
@Tag(name = "服务器ssh连接信息")
@RequiredArgsConstructor
public class SshInfoController  {

    private final SshInfoService sshInfoService;


    @GetMapping("/{id}")
    @Operation(summary = "根据ID获取详情", description = "save")
    public ResultVO<SshInfo> info(@PathVariable("id") Long id) {
        SshInfo bean = sshInfoService.findOnly("id", id).orElse(new SshInfo());
        return ResultVO.success( bean);
    }

    @Operation(summary = "新增服务器ssh连接信息")
    @PostMapping("append")
    public ResultVO<String> append(@RequestBody @Valid SshInfoAdd append) {
        sshInfoService.saveOneByVo(append);
        return ResultVO.success();
    }

    @Operation(summary = "分页查询")
    @PostMapping("page")
    public ResultPageVO<SshInfo,JpaPageResult<SshInfo>> page(@RequestBody @Valid SshInfoPage page) {
        Page<SshInfo> byBean =  sshInfoService.findPage(page, page.getPage());
        JpaPageResult<SshInfo> pageResult = JpaPageResult.toPage(byBean);
        return ResultPageVO.success(pageResult, "查询成功");
    }

    @Operation(summary = "集合")
    @GetMapping("lists")
    public ResultVO<List<SshInfo>> lists() {
         List<SshInfo> finds = sshInfoService.finds();
        return ResultVO.success(ListTo.to(SshInfo.class,finds));
    }


    @Operation(summary = "删除服务器ssh连接信息")
    @PostMapping("delete")
    @Parameter(name = "ids", description = "id集合", required = true)
    public ResultVO<String> delete(@RequestBody List<Long> ids) {
        sshInfoService.delete("id", SQLOperator.IN, ids);
        return ResultVO.success();
    }

    @Operation(summary = "编辑服务器ssh连接信息")
    @PostMapping("edit")
    public ResultVO<String> edit(@RequestBody  @Valid SshInfoEdit edit)  {
        sshInfoService.update(edit, SQLOperator.EQ);
        return ResultVO.success();
    }
}
